﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace yule.Util
{

    /// <summary>
    /// 运行时间
    /// </summary>
    public class RunTime
    {
        /// <summary>
        /// 计算类型
        /// </summary>
        public enum ToTalType
        {
            /// <summary>
            /// 秒
            /// </summary>
            TotalSeconds,
            /// <summary>
            /// 毫秒
            /// </summary>
            TotalMilliseconds
        }
        private DateTime _StartTime;
        private DateTime _NextTime;

        public RunTime()
        {
            this._StartTime = DateTime.Now;
            this._NextTime = DateTime.Now;
        }

        /// <summary>
        /// 获取总运行时间（毫秒）
        /// RunTime.ToTalType.TotalMilliseconds
        /// </summary>
        /// <returns></returns>
        public double GetTotal()
        {
            return this.GetTotal(RunTime.ToTalType.TotalMilliseconds, false);
        }

        /// <summary>
        /// 获取运行时间（毫秒）
        /// RunTime.ToTalType.TotalMilliseconds
        /// </summary>
        /// <param name="isGetCurrentTime">是否获取上次间隔</param>
        /// <returns></returns>
        public double GetTotal(bool isGetCurrentTime)
        {
            return this.GetTotal(RunTime.ToTalType.TotalMilliseconds, isGetCurrentTime);
        }

        /// <summary>
        /// 获取总运行时间
        /// </summary>
        /// <param name="iType"></param>
        /// <returns></returns>
        public double GetTotal(RunTime.ToTalType iType)
        {
            return this.GetTotal(iType, false);
        }

        /// <summary>
        /// 获取运行时间
        /// </summary>
        /// <param name="iType">类型</param>
        /// <param name="isOneTest">是否获取上次间隔</param>
        /// <returns></returns>
        public double GetTotal(RunTime.ToTalType iType, bool isOneTest)
        {
            TimeSpan timeSpan = isOneTest ? (DateTime.Now - this._NextTime) : (DateTime.Now - this._StartTime);
            this._NextTime = DateTime.Now;
            double result;
            if (iType != RunTime.ToTalType.TotalMilliseconds)
            {
                result = timeSpan.TotalSeconds;
            }
            else
            {
                result = timeSpan.TotalMilliseconds;
            }
            return result;
        }

    }

}
